package com.seselin.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.seselin.bean.SortItemParam;
import com.seselin.bean.SortTitleBean;

import java.util.ArrayList;

/**
 * Created by Seselin on 2018/7/19 13:37.
 */

public class SortTitleView extends LinearLayout {

    private ArrayList<SortTitleBean> itemList;
    private ArrayList<View> itemViews;
    private SortItemParam itemParam = new SortItemParam();

    public SortTitleView(Context context) {
        super(context);
    }

    public SortTitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SortTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private OnClickListener listener;

    public interface OnClickListener {
        void onConfirmClick(int position, int sortTag);
    }

    public void setOnClickListener(OnClickListener listener) {
        this.listener = listener;
    }


    /**
     * @param itemParam 标题样式自定义配置，需要数据初始化前调用
     */
    public void setItemParam(SortItemParam itemParam) {
        this.itemParam = itemParam;
    }

    /**
     * @param titles 标题名称列表
     */
    public void setNameList(ArrayList<String> titles) {
        itemList = new ArrayList<>();
        for (String title : titles) {
            itemList.add(new SortTitleBean(title));
        }
        setItemView();
    }

    /**
     * @param itemList 定制化标题列表
     */
    public void setItemList(ArrayList<SortTitleBean> itemList) {
        this.itemList = itemList;
        setItemView();
    }

    /**
     * @param index 初始化需要排序的标题序号
     */
    public void initView(int index) {
        if (itemViews != null && index < itemViews.size()) {
            onItemClick(index);
        }
    }

    private void setItemView() {
        itemViews = new ArrayList<>();
        removeAllViews();
        for (SortTitleBean titleItemBean : itemList) {
            View itemView = LayoutInflater.from(getContext()).inflate(itemParam.getLayoutRes(), this, false);
            TextView tvName = itemView.findViewById(itemParam.getTvTitleId());
            tvName.setText(titleItemBean.getName());
            setSortView(itemView, titleItemBean);
            itemViews.add(itemView);
            addView(itemView);
        }

        for (int i = 0; i < itemViews.size(); i++) {
            setItemClickListener(i);
        }
    }

    private void setItemClickListener(final int index) {
        itemViews.get(index).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClick(index);
            }
        });
    }

    private void onItemClick(int index) {
        for (int i = 0; i < itemList.size(); i++) {
            SortTitleBean bean = itemList.get(i);
            if (i == index) {
                if (!bean.isCheck()) {
                    bean.setCheck(true);
                } else {
                    bean.changSortTag();
                }
                if (listener != null) {
                    listener.onConfirmClick(index, bean.getSortTag());
                }
            } else {
                bean.setCheck(false);
            }
            setSortView(itemViews.get(i), bean);
        }
    }

    /**
     * 设置单个标题的UI
     *
     * @param itemView 标题的UI
     * @param bean     标题的数据
     */
    private void setSortView(View itemView, SortTitleBean bean) {
        TextView tvName = itemView.findViewById(itemParam.getTvTitleId());
        ImageView ivSort = itemView.findViewById(itemParam.getIvSortId());
        if (!bean.isCheck()) {
            ivSort.setImageResource(itemParam.getIconNullRes());
            tvName.setTextColor(getContext().getResources().getColor(itemParam.getUnCheckColor()));
            return;
        }
        tvName.setTextColor(getContext().getResources().getColor(itemParam.getCheckColor()));
        switch (bean.getSortTag()) {
            case 1:
                ivSort.setImageResource(itemParam.getIconUpRes());
                break;
            case -1:
                ivSort.setImageResource(itemParam.getIconDownRes());
                break;
            default:
                ivSort.setImageResource(itemParam.getIconUpRes());
                break;
        }
    }

}
